package com.victor.zjbj_android.yinhuan.fragment;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.victor.zjbj_android.R;
import com.victor.zjbj_android.base.BaseFragment;
import com.victor.zjbj_android.bean.YhtypeListBean;
import com.victor.zjbj_android.bean.YinhuanListBean;
import com.victor.zjbj_android.mod.zhongxin.XiugaiMimaActivity;
import com.victor.zjbj_android.utils.FastJsonTools;
import com.victor.zjbj_android.utils.LoginUtils;
import com.victor.zjbj_android.utils.MainUtils;
import com.victor.zjbj_android.utils.OnGlobalListener;
import com.victor.zjbj_android.utils.RecyclerViewUtils;
import com.victor.zjbj_android.utils.ZjbjManager;
import com.victor.zjbj_android.utils.retrofit.BaseSubscriber;
import com.victor.zjbj_android.utils.retrofit.ExceptionHandle;
import com.victor.zjbj_android.utils.retrofit.RetrofitUtils;
import com.victor.zjbj_android.yinhuan.NewYinhuanActivity;
import com.victor.zjbj_android.yinhuan.YinHuanInfoActivity;
import com.victor.zjbj_android.yinhuan.YinHuanTiJiaoActivity;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.util.DensityUtil;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.ArrayList;

public class YihuanListFragment extends BaseFragment {
    private RecyclerView rv_list;
    private ArrayList m_list;
    private int m_curType;
    public YihuanListFragment() {
        // Required empty public constructor
    }

    @Override
    protected int setLayout(LayoutInflater inflater) {
        return R.layout.fragment_yihuan_list;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    protected void onFirstUserVisible() {

    }

    private void initView() {
        Bundle bundle = getArguments();
        if(bundle != null)
        {
            m_curType = bundle.getInt("type");
        }

        rv_list=mMainView.findViewById(R.id.rv_list);

        x.view().inject(getActivity());


        LogUtils.e("onViewCreated" );
        initData(m_curType);
    }
    @Override
    protected void onUserInvisible() {

    }
    @Override
    protected void onFirstUserInvisble() {

    }

    @Override
    protected void onUserVisble() {

    }
    @Override
    protected void init(Bundle savedInstanceState) {
        initView();
    }


    //原始方法
    // TODO: Rename and change types and number of parameters
    public static YihuanListFragment newInstance(int state) {
        YihuanListFragment fragment = new YihuanListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", state);
        fragment.setArguments(bundle);
        return fragment;
    }


    public void refresh(int itype)
    {
        LogUtils.e("fragment refresh:" + itype);
        //initData(itype);
    }

    public void initData(int itype)
    {
        m_list=new ArrayList();


        RetrofitUtils mRetrofit1 = RetrofitUtils.getInstance(getActivity().getApplicationContext()).createBaseApi();
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("author_sn", LoginUtils.getLoginConfig(getActivity().getApplicationContext()).getSn());
            if(itype != 0)
            {
                jsonObject.put("phone_status", itype);
            }
        }catch (Exception e){

        }


        ZjbjManager tzsbManager=new ZjbjManager();

        tzsbManager.getYinhuanList(jsonObject.toString(), MainUtils.getToken(getActivity().getApplicationContext()), mRetrofit1, new BaseSubscriber() {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {

            }

            @Override
            public void onSuccess(JSONObject jsonObject) throws JSONException {
                YinhuanListBean requestBean= FastJsonTools.getBean(jsonObject.toString(),YinhuanListBean.class);
                if(requestBean.getCode().equals("0")){
                    if (m_list.size() > 0) {
                        m_list.clear();
                    }
                    m_list = (ArrayList<YinhuanListBean.DataBean.DataItemBean>) requestBean.getData().getDataItem();

                    if(m_list.size() >= 0) {
                        RecyclerViewUtils.initLiner(getActivity(), rv_list, R.layout.item_yinhuan, m_list, new OnGlobalListener() {
                            @Override
                            public <T> void logic(BaseViewHolder helper, T item) {
                                YinhuanListBean.DataBean.DataItemBean bean = (YinhuanListBean.DataBean.DataItemBean) item;
                                helper.setText(R.id.tv_dname, bean.getYh_team_str());
                                helper.setText(R.id.tv_content, bean.getYh_remark());

                                String sT = bean.getYh_type();
                                ArrayList yhtypeList = ((YinHuanTiJiaoActivity) getActivity()).alYhtype;
                                for (Object tp1 : yhtypeList) {
                                    if (((YhtypeListBean.DataBean.DataItemBean) tp1).getId().toString().equals(sT))
                                        helper.setText(R.id.tv1, ((YhtypeListBean.DataBean.DataItemBean) tp1).getTypename());
                                }

                                if (bean.getYh_dealtype().equals("1"))
                                    helper.setText(R.id.tv2, "现场治理");
                                if (bean.getYh_dealtype().equals("2"))
                                    helper.setText(R.id.tv2, "限时整改");
                                if (bean.getYh_dealtype().equals("3"))
                                    helper.setText(R.id.tv2, "挂牌督办");

                                if (bean.getStatus() == 2) {
                                    helper.setText(R.id.tv_status, "待确认");
                                }
                                if (bean.getStatus() == 3) {
                                    helper.setText(R.id.tv_status, "整改中");
                                }
                                if (bean.getStatus() == 4) {
                                    helper.setText(R.id.tv_status, "验收中");
                                }
                                if (bean.getStatus() == 5) {
                                    helper.setText(R.id.tv_status, "已完成");
                                }
                                if (bean.getStatus() == 6) {
                                    helper.setText(R.id.tv_status, "已驳回");
                                }
                                if (bean.getStatus() == 7) {
                                    helper.setText(R.id.tv_status, "暂停申请中");
                                }
                                if (bean.getStatus() == 8) {
                                    helper.setText(R.id.tv_status, "暂停中");
                                }
                                if (bean.getStatus() == 9) {
                                    helper.setText(R.id.tv_status, "已超时");
                                }

                                if (StringUtils.isEmpty(bean.getPrj_img_url())) {
                                    helper.setImageResource(R.id.tv_pic, R.drawable.noprjimg);
                                } else {
                                    ImageView ivPrj = helper.getView(R.id.tv_pic);
                                    ImageOptions imageOptions = new ImageOptions.Builder()
                                            .setRadius(DensityUtil.dip2px(5))//ImageView圆角半径
                                            .setCrop(true)// 如果ImageView的大小不是定义为wrap_content, 不要crop.
                                            .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
                                            .setFailureDrawableId(R.drawable.noprjimg)//加载失败后默认显示图片
                                            .build();
                                    x.image().bind(ivPrj, RetrofitUtils.Base_URL + bean.getPrj_img_url());
                                }

                            }
                        }, new BaseQuickAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                startActivity(new Intent(getActivity(), YinHuanInfoActivity.class));
                            }
                        });
                    }
                    else
                    {
                        ToastUtils.showShort("没有数据");
                    }

                }else {
                    ToastUtils.showShort(requestBean.getInfo());
                }
            }
        });


    }

    /*
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_yihuan_list, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        rv_list=view.findViewById(R.id.rv_list);

        x.view().inject(getActivity());


        LogUtils.e("onViewCreated" );
    }
    */

}
